package com.changpei.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changpei.common.result.Result;
import com.changpei.core.pojo.File;
import com.changpei.core.service.FileService;
import com.changpei.core.utils.SearchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>Title : File</p>
 * <p>Description : file的控制层</p>
 *
 * @author weibiao
 * @date 2020年3月7日
 */

@RestController
@RequestMapping("/file")
@CrossOrigin
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * <p>Title : add</p>
     * <p>Description : 新增File数据</p>
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/add")
    public Result<Long> add(@RequestBody File file) {
        // 调用FileService实现添加File
        if (fileService.save(file)) {
            return Result.ok(file.getId());
        }
        return Result.failed();
    }

    /**
     * <p>Title : delete</p>
     * <p>Description : File根据ID数据</p>
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable Long id) {
        // 调用FileService实现根据主键删除
        if (fileService.removeById(id)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : update</p>
     * <p>Description : File根据ID修改</p>
     *
     * @param file
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody File file, @PathVariable Long id) {
        // 设置主键值
        file.setId(id);
        // 调用FileService实现修改File
        if (fileService.updateById(file)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : File分页搜索实现</p>
     *
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<File>> findPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用FileService实现分页查询File
        Page<File> pageInfo = fileService.page(new Page<>(pageNum, pageSize));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : File分页条件搜索实现</p>
     *
     * @param file
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @PostMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<File>> findPage(@RequestBody(required = false) File file, @PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用FileService实现分页条件查询File
        Page<File> pageInfo = fileService.page(new Page<>(pageNum, pageSize), SearchUtil.initQuery(file));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findList</p>
     * <p>Description : 多条件搜索File数据</p>
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<File>> findList(@RequestBody(required = false) File file) {
        // 调用FileService实现条件查询File
        List<File> list = fileService.list(SearchUtil.initQuery(file));
        return Result.ok(list);
    }

    /**
     * <p>Title : findById</p>
     * <p>Description : 根据ID查询File数据</p>
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<File> findById(@PathVariable Long id) {
        // 调用FileService实现根据主键查询File
        File file = fileService.getById(id);
        return Result.ok(file);
    }

    /**
     * <p>Title : findOne</p>
     * <p>Description : 多条件搜索File数据，结果返回一条</p>
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/findOne")
    public Result<File> findOne(@RequestBody(required = false) File file) {
        // 调用FileService实现条件查询File
        File fileResult = fileService.getOne(SearchUtil.initQuery(file));
        return Result.ok(fileResult);
    }

    /**
     * 根据id查询集合
     *
     * @param ids
     * @return
     */
    @GetMapping(value = "/findByIds")
    public Result<List<File>> findByIds(@RequestParam(required = false, value = "ids") List<Long> ids) {
        List<File> list = fileService.listByIds(ids);
        return Result.ok(list);
    }

    /**
     * <p>Title : findAll</p>
     * <p>Description : 查询File全部数据</p>
     *
     * @return
     */
    @GetMapping(value = "/all")
    public Result<List<File>> findAll() {
        // 调用FileService实现查询所有File
        List<File> list = fileService.list();
        return Result.ok(list);
    }
}
